/*
 * Copyright (c) 2011, Will Strohl
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list 
 * of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, this 
 * list of conditions and the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.
 * 
 * Neither the name of WillStrohl.com nor the names of its contributors may be used 
 * to endorse or promote products derived from this software without specific prior 
 * written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.IO;
using  System.Net;
using DotNetNuke.Services.Exceptions;

namespace DotNetNuke.Modules.WillStrohlDisqus.Components
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Controller class for WillStrohlDisqus
    /// </summary>
    /// -----------------------------------------------------------------------------
    public sealed class FeatureController /*: IPortable, ISearchable, IUpgradeable*/
    {

        #region Public Methods



        #endregion

        /// <summary>
        /// GetContent - a method that imports REST content
        /// </summary>
        /// <param name="requestUrl">the REST location</param>
        /// <returns></returns>
        public string GetContent(string requestUrl)
        {
            return GetContent(requestUrl, string.Empty, -1);
        }

        /// <summary>
        /// GetContent - a method that imports REST content
        /// </summary>
        /// <param name="requestUrl">the REST location</param>
        /// <param name="proxyAddress">address for the proxy server</param>
        /// <param name="proxyPort">port for the proxy server</param>
        /// <returns></returns>
        public string GetContent(string requestUrl, string proxyAddress, int proxyPort)
        {

            string strReturn = string.Empty;
            WebRequest request = null;
            HttpWebResponse response = null;
            Stream dataStream = null;
            StreamReader reader = null;

            try
            {

                // Create a request for the URL.         
                request = WebRequest.Create(requestUrl);
                if (!string.IsNullOrEmpty(proxyAddress) && proxyPort > -1)
                {
                    request.Proxy = new WebProxy(string.Concat(proxyAddress, ":", proxyPort.ToString()), true);
                }
                else
                {
                    request.Proxy = null;
                }

                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;

                // Get the response.
                response = (HttpWebResponse)request.GetResponse();

                // Display the status.
                // Console.WriteLine (response.StatusDescription);

                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                reader = new StreamReader(dataStream);

                strReturn = reader.ReadToEnd();

            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                throw ex;
            }
            finally
            {
                // Cleanup the streams and the response.
                reader.Close();
                dataStream.Close();
                response.Close();
            }

            return strReturn;

        }

        /*
        #region Optional Interfaces

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ExportModule implements the IPortable ExportModule Interface
        /// </summary>
        /// <param name="ModuleID">The Id of the module to be exported</param>
        /// -----------------------------------------------------------------------------
        public string ExportModule(int ModuleID)
        {
            //string strXML = "";

            //List<WillStrohlDisqusInfo> colWillStrohlDisquss = GetWillStrohlDisquss(ModuleID);
            //if (colWillStrohlDisquss.Count != 0)
            //{
            //    strXML += "<WillStrohlDisquss>";

            //    foreach (WillStrohlDisqusInfo objWillStrohlDisqus in colWillStrohlDisquss)
            //    {
            //        strXML += "<WillStrohlDisqus>";
            //        strXML += "<content>" + DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objWillStrohlDisqus.Content) + "</content>";
            //        strXML += "</WillStrohlDisqus>";
            //    }
            //    strXML += "</WillStrohlDisquss>";
            //}

            //return strXML;

            throw new System.NotImplementedException("The method or operation is not implemented.");
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ImportModule implements the IPortable ImportModule Interface
        /// </summary>
        /// <param name="ModuleID">The Id of the module to be imported</param>
        /// <param name="Content">The content to be imported</param>
        /// <param name="Version">The version of the module to be imported</param>
        /// <param name="UserId">The Id of the user performing the import</param>
        /// -----------------------------------------------------------------------------
        public void ImportModule(int ModuleID, string Content, string Version, int UserID)
        {
            //XmlNode xmlWillStrohlDisquss = DotNetNuke.Common.Globals.GetContent(Content, "WillStrohlDisquss");
            //foreach (XmlNode xmlWillStrohlDisqus in xmlWillStrohlDisquss.SelectNodes("WillStrohlDisqus"))
            //{
            //    WillStrohlDisqusInfo objWillStrohlDisqus = new WillStrohlDisqusInfo();
            //    objWillStrohlDisqus.ModuleId = ModuleID;
            //    objWillStrohlDisqus.Content = xmlWillStrohlDisqus.SelectSingleNode("content").InnerText;
            //    objWillStrohlDisqus.CreatedByUser = UserID;
            //    AddWillStrohlDisqus(objWillStrohlDisqus);
            //}

            throw new System.NotImplementedException("The method or operation is not implemented.");
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetSearchItems implements the ISearchable Interface
        /// </summary>
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param>
        /// -----------------------------------------------------------------------------
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo)
        {
            //SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            //List<WillStrohlDisqusInfo> colWillStrohlDisquss = GetWillStrohlDisquss(ModInfo.ModuleID);

            //foreach (WillStrohlDisqusInfo objWillStrohlDisqus in colWillStrohlDisquss)
            //{
            //    SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objWillStrohlDisqus.Content, objWillStrohlDisqus.CreatedByUser, objWillStrohlDisqus.CreatedDate, ModInfo.ModuleID, objWillStrohlDisqus.ItemId.ToString(), objWillStrohlDisqus.Content, "ItemId=" + objWillStrohlDisqus.ItemId.ToString());
            //    SearchItemCollection.Add(SearchItem);
            //}

            //return SearchItemCollection;

            throw new System.NotImplementedException("The method or operation is not implemented.");
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpgradeModule implements the IUpgradeable Interface
        /// </summary>
        /// <param name="Version">The current version of the module</param>
        /// -----------------------------------------------------------------------------
        public string UpgradeModule(string Version)
        {
            throw new System.NotImplementedException("The method or operation is not implemented.");
        }

        #endregion
        */

    }

}
